package com.xhui.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhui.reggie.common.R;
import com.xhui.reggie.dto.DishDTO;
import com.xhui.reggie.entity.Dish;
import com.xhui.reggie.entity.DishFlavor;
import com.xhui.reggie.service.CategoryService;
import com.xhui.reggie.service.DishFlavorService;
import com.xhui.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private DishFlavorService dishFlavorService;

    @Resource
    private CategoryService categoryService;

    /**
     * 新增菜品
     *
     * @param dishDTO 前端数据封装实体类
     * @return 新增是否成功
     */
    @PostMapping
    public R<String> save(@RequestBody DishDTO dishDTO) {
        log.info("dishDTO saved {}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        return R.success("新增菜品成功");
    }

    /*老师写的
   @GetMapping("/page")
   public R<Page<DishDTO>> getPage(int page, int pageSize, String name){

       // 1. 封装分页
       Page<Dish> dishPage = new Page<>(page, pageSize);

       // 2. 封装查询条件
       LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
       // 2.1是否存在模糊查询的字段
       dishLambdaQueryWrapper.like(StringUtils.isNotBlank(name), Dish::getName, name);
       dishLambdaQueryWrapper.orderByAsc(Dish::getSort);
       dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

       // 3.执行分页查询
       dishService.page(dishPage, dishLambdaQueryWrapper);

       // 对象属性拷贝 records属性忽略掉
       Page<DishDTO> dishDTOPage = new Page<>();

       BeanUtils.copyProperties(dishPage, dishDTOPage, "records");

       List<Dish> dishRecords = dishPage.getRecords();
       List<DishDTO> dishDTORecords = dishRecords.stream().map(item -> {

           DishDTO dishDTO = new DishDTO();
           // 普通信息放置到dishDTO中
           BeanUtils.copyProperties(item, dishDTO);

           Long categoryId = item.getCategoryId(); // 分类id
           // 根据id查询分类名称
           Category category = categoryService.getById(categoryId);
           String categoryName = category.getName();
           // dishDTO设置菜品名称
           dishDTO.setCategoryName(categoryName);

           return dishDTO;

       }).collect(Collectors.toList());

       dishDTOPage.setRecords(dishDTORecords);
       return R.success(dishDTOPage);
   }  */

    /**
     * 查询菜品详细信息 by myself
     *
     * @param page     页码
     * @param pageSize 页面条数
     * @param name     模糊菜品名称
     * @return 实体类
     */
    @GetMapping("/page")
    public R<Page<Dish>> getPage(int page, int pageSize, String name) {

        // 1. 封装分页
        Page<Dish> dishPage = new Page<>(page, pageSize);

        // 2. 封装查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 2.1是否存在模糊查询的字段
        dishLambdaQueryWrapper.like(StringUtils.isNotBlank(name), Dish::getName, name);
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        // 3.执行分页查询
        dishService.page(dishPage, dishLambdaQueryWrapper);
        // 因为records中存categoryName属性为空，
        // 需通过查询另外一个表category根据id查询到categoryName，然后填充到records中去
        List<Dish> records = dishPage.getRecords();
        records.stream().forEach(dish -> {
            dish.setCategoryName(categoryService.getById(dish.getCategoryId()).getName());
        });

        return R.success(dishPage);
    }

    /**
     * 返回指定id的菜品信息
     * @param id dish id
     * @return 返回指定id的菜品信息
     */
    @GetMapping("/{id}")
    public R<DishDTO> getById(@PathVariable Long id) {
        DishDTO dishDTO = dishService.getByIdWithFlavor(id);
        return R.success(dishDTO);
    }

    /**
     * 修改菜品数据
     * @param dishDTO 前端对象
     * @return 是否成功
     */
    @PutMapping
    public R<String> update(@RequestBody DishDTO dishDTO){
        dishService.updateWithFlavor(dishDTO);
        return R.success("菜品修改成功");
    }

    /**
     * 批量启用/禁用 菜品的的状态
     * @param status 菜品状态标识
     * @param ids 菜品ids
     * @return 提示信息
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status, String ids){
        dishService.updateStatus(status, ids);
        return R.success("批量操作成功");
    }

    /**
     *
     * 根据指定 分类id 查询多个菜品信息
     * @param dish 分类信息
     * @return 指定分类多个菜品信息
     */
   /* @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        // 构造查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询指定菜品分类的id
        dishLambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        // 只能查到起售状态(status为 1)的数据
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        dishLambdaQueryWrapper.like(StringUtils.isNotBlank(dish.getName()), Dish::getName, dish.getName());
        // 添加排序条件
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);
        return R.success(dishList);
    }*/

    /**
     * 根据 分类id 查询 启售菜品 的全部信息
     * @param dish --> categoryId and status
     * @return 菜品的全部信息
     */
    @GetMapping("/list")
    public R<List<DishDTO>> list(Dish dish){
        // 构造查询条件
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询指定菜品分类的id
        dishLambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        // 只能查到起售状态(status为 1)的数据
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        dishLambdaQueryWrapper.like(StringUtils.isNotBlank(dish.getName()), Dish::getName, dish.getName());
        // 添加排序条件
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        // 查询到的菜品基本信息
        List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);

        List<DishDTO> dishDTOList = new ArrayList<>();
        dishList.stream().forEach(item ->{

            // 直接根据dishId 查询对应菜品的若干个口味信息
            DishDTO dishDTO1 = dishService.getByIdWithFlavor(item.getId());
            dishDTOList.add(dishDTO1);

            /*DishDTO dishDTO = new DishDTO();
            // 获取到菜品的口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);

            dishDTO.setFlavors(dishFlavors); // 封装某个菜品的若干个口味信息
            BeanUtils.copyProperties(item, dishDTO); // 封装每个菜品的详细信息
            dishDTOList.add(dishDTO);*/
        });

        return R.success(dishDTOList);
    }
}
